package cn.zn.modules.budget.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.zn.modules.budget.cur.dao.TBudgetInfoMapper;
import cn.zn.modules.budget.cur.entity.TBudgetInfo;
import cn.zn.modules.budget.cur.entity.TBudgetInfoDetailEntity;
import cn.zn.modules.budget.cur.entity.vo.BudgetInfoDeptNameSumVO;
import cn.zn.modules.budget.cur.entity.vo.BudgetInfoDeptSumVO;
import cn.zn.modules.budget.cur.entity.vo.BudgetInfoSumVO;
import cn.zn.modules.budget.cur.service.TBudgetInfoDetailService;
import cn.zn.modules.budget.cur.service.TBudgetInfoService;
import cn.zn.modules.budget.inter.BudgetInfoInter;
import cn.zn.modules.budget.inter.vo.BudgetInfoVO;
import cn.zn.modules.budget.vo.BudgetMoneyVO;
import cn.zn.modules.zzsys.inter.DeptInter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TBudgetInfoServiceImpl extends ServiceImpl<TBudgetInfoMapper, TBudgetInfo> implements TBudgetInfoService, BudgetInfoInter {

    @Autowired
    private TBudgetInfoDetailService tBudgetInfoDetailService;
    @Autowired
    private DeptInter deptInter;

    @Override
    public List<BudgetInfoVO> findByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return CollUtil.newArrayList();
        }
        return this.baseMapper.findList(ids).stream().map(o -> {
            BudgetInfoVO budgetInfoVO = new BudgetInfoVO();
            BeanUtils.copyProperties(o, budgetInfoVO);
            return budgetInfoVO;
        }).collect(Collectors.toList());
    }

    @Override
    public long findCountToAudit() {
        return tBudgetInfoDetailService.count(new QueryWrapper<TBudgetInfoDetailEntity>().eq("STATE_", 0));
    }

    @Override
    public BudgetMoneyVO findAvailableBudget(String deptCode, String projectCode, String cateCode) {
        BudgetMoneyVO budgetMoney = this.baseMapper.findBudgetMoney(deptCode, projectCode, cateCode);
        if (budgetMoney == null) {
            // 小类没有预算时，查询大类
            budgetMoney = this.baseMapper.findBudgetMoney(deptCode, projectCode, cateCode.substring(0, 3));
        }
        return budgetMoney;
    }

    @Override
    public List<BudgetInfoSumVO> querySum(String deptCode, String projectName, String cateName) {
        return baseMapper.querySum(deptCode, projectName, cateName);
    }

    @Override
    public List<BudgetInfoDeptSumVO> querySumForDept(Integer year) {
        return this.baseMapper.querySumForDept(year);
    }

    @Override
    public List<BudgetInfoDeptNameSumVO> querySumForDeptAll(Integer year) {
        // 查询已设置的预算
        Map<String, BudgetInfoDeptSumVO> setupMap = this.baseMapper.querySumForDept(year).stream().collect(Collectors.toMap(BudgetInfoDeptSumVO::getDeptCode, o -> o));
        // 查询所有部门
        return deptInter.queryForLeaf().stream().map(o -> {
            BudgetInfoDeptNameSumVO vo = new BudgetInfoDeptNameSumVO();
            vo.setDeptCode(o.getDeptCode());
            vo.setDeptName(o.getDeptName());
            if (setupMap.containsKey(o.getDeptCode())) {
                BudgetInfoDeptSumVO budgetInfoDeptSumVO = setupMap.get(o.getDeptCode());
                vo.setTotalMoney(budgetInfoDeptSumVO.getTotalMoney());
                vo.setFreezeMoney(budgetInfoDeptSumVO.getFreezeMoney());
                vo.setUseMoney(budgetInfoDeptSumVO.getUseMoney());
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, String>> findSGByInfo(List<String> budgetInfoIds) {
        if (budgetInfoIds == null || budgetInfoIds.size() == 0) {
            return CollUtil.newArrayList();
        }
        return this.baseMapper.findSGForInfo(budgetInfoIds);
    }
}
